package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 分页查询，查询所有用户信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @return
     */
    @RequestMapping("/findAllUserInfo")
    public ModelAndView findAllUserInfo(
            @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", required = true, defaultValue = "3") Integer pageSize) {
        //1、分页查询所有数据
        List<UserInfo> userInfoList = userInfoService.findAllUserInfo(pageNum, pageSize);
        //2、封装分页数据
        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(userInfoList);
        ModelAndView mav = new ModelAndView();
        mav.addObject("userInfo", pageInfo);
        mav.setViewName("user-list");
        return mav;
    }

    /**
     * 添加用户信息
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("/insertUserInfo")
    public String insertUserInfo(UserInfo userInfo) {
        userInfoService.insertUserInfo(userInfo);
        return "redirect:/userInfo/findAllUserInfo";
    }

    /**
     * 根据用户id查询详情信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findByUserInfoId")
    public String findByUserInfoId(String id, Model model) {
        UserInfo userInfoId = userInfoService.findByUserInfoId(id);
        System.out.println("userInfoId = " + userInfoId);
        model.addAttribute("userShow", userInfoId);
        return "user-show";
    }


    /**
     * 根据用户id查询此用户未拥有的角色
     *
     * @param userInfoId 用户id
     * @param model
     * @return
     */
    @RequestMapping("/findUserInfoRole")
    public String findUserInfoRole(@RequestParam(name = "id", required = true) String userInfoId, Model model) {
        List<Role> roleList = userInfoService.insertUserInfoRole(userInfoId);
        model.addAttribute("userInfoId", userInfoId);//把用户id存入共享域
        model.addAttribute("role", roleList);
        return "user-role-add";
    }


    /**
     * 根据用户id和角色id，添加此用户未拥有的角色
     *
     * @param userInfoId 用户ID
     * @param roleIds    角色ID
     * @return
     */
    @RequestMapping("/insertByUserInfoIdAndRoleId")
    public String insertByUserInfoIdAndRoleId(
            @RequestParam(name = "userId", required = true) String userInfoId,
            @RequestParam(name = "ids", required = true) String[] roleIds
    ) {
        userInfoService.insertByUserInfoIdAndRoleId(userInfoId,roleIds);
        return "redirect:/userInfo/findAllUserInfo";
    }


}
